package com.ruoyi.gxs.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.gxs.domain.GxsComprehensive;
import com.ruoyi.gxs.mapper.GxsComprehensiveMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.gxs.mapper.GxsSportsMapper;
import com.ruoyi.gxs.domain.GxsSports;
import com.ruoyi.gxs.service.IGxsSportsService;

/**
 * 体美劳得分Service业务层处理
 * 
 * @author 高晓松
 * @date 2024-01-03
 */
@Service
public class GxsSportsServiceImpl implements IGxsSportsService 
{
    @Autowired
    private GxsSportsMapper gxsSportsMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private GxsComprehensiveMapper comprehensiveMapper;

    /**
     * 查询体美劳得分
     * 
     * @param gxsId 体美劳得分主键
     * @return 体美劳得分
     */
    @Override
    public GxsSports selectGxsSportsByGxsId(Long gxsId)
    {
        return gxsSportsMapper.selectGxsSportsByGxsId(gxsId);
    }

    /**
     * 查询体美劳得分列表
     * 
     * @param gxsSports 体美劳得分
     * @return 体美劳得分
     */
    @Override
    public List<GxsSports> selectGxsSportsList(GxsSports gxsSports)
    {
        return gxsSportsMapper.selectGxsSportsList(gxsSports);
    }

    /**
     * 新增体美劳得分
     * 
     * @param gxsSports 体美劳得分
     * @return 结果
     */
    @Override
    public int insertGxsSports(GxsSports gxsSports)
    {
        //根据学号查询 昵称、用户ID、部门 并添加到gxsStudyChange
        SysUser sysUser = sysUserMapper.selectUserByUserName(gxsSports.getUserName());
        gxsSports.setNickName(sysUser.getNickName());
        gxsSports.setUserId(sysUser.getUserId());
        gxsSports.setDeptId(sysUser.getDeptId());

        //根据新增的专业分数信息 查询综合得分学习成绩
        GxsComprehensive comprehensive = comprehensiveMapper.selectGxsComprehensiveByUserName(gxsSports.getUserName());
        gxsSports.setGxsSportsRecord(comprehensive.getGxsSportsscore());

        //根据加分状态对学习成绩进行加减
        long totalPoints =0L;
        if (gxsSports.getGxsBonusState() == 0L){
            totalPoints = gxsSports.getGxsSportsRecord() + gxsSports.getGxsScore();
        }
        else {
            totalPoints = gxsSports.getGxsSportsRecord() - gxsSports.getGxsScore();
        }
        gxsSports.setGxsTotalPoints(totalPoints);
        comprehensive.setGxsSportsscore(totalPoints);
        comprehensiveMapper.updateGxsComprehensive(comprehensive);

        gxsSports.setCreateTime(DateUtils.getNowDate());
        return gxsSportsMapper.insertGxsSports(gxsSports);
    }

    /**
     * 修改体美劳得分
     * 
     * @param gxsSports 体美劳得分
     * @return 结果
     */
    @Override
    public int updateGxsSports(GxsSports gxsSports)
    {
        gxsSports.setUpdateTime(DateUtils.getNowDate());
        return gxsSportsMapper.updateGxsSports(gxsSports);
    }

    /**
     * 批量删除体美劳得分
     * 
     * @param gxsIds 需要删除的体美劳得分主键
     * @return 结果
     */
    @Override
    public int deleteGxsSportsByGxsIds(Long[] gxsIds)
    {
        return gxsSportsMapper.deleteGxsSportsByGxsIds(gxsIds);
    }

    /**
     * 删除体美劳得分信息
     * 
     * @param gxsId 体美劳得分主键
     * @return 结果
     */
    @Override
    public int deleteGxsSportsByGxsId(Long gxsId)
    {
        return gxsSportsMapper.deleteGxsSportsByGxsId(gxsId);
    }
}
